package com.hbnu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hbnu.mapper.UserMapper;
import com.hbnu.pojo.OTD.LoginResponse;
import com.hbnu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    //获取用户信息
    @GetMapping("/info")
    public LoginResponse getUserInfo(@RequestParam String username) {
        User user = userMapper.selectOne(
                new QueryWrapper<User>().eq("username", username));
        if (user != null) {
            user.setPassword("******"); // 前端显示用，不返回真实密码
            return new LoginResponse(true, "查询成功", user);
        }
        return new LoginResponse(false, "用户不存在", null);
    }

    //修改密码
    @PostMapping("/updatePassword")
    public LoginResponse updatePassword(@RequestBody Map<String, String> req) {
        String username = req.get("username");
        String oldPassword = req.get("oldPassword");
        String newPassword = req.get("newPassword");

        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if (user == null) {
            return new LoginResponse(false, "用户不存在", null);
        }
        if (!user.getPassword().equals(oldPassword)) {
            return new LoginResponse(false, "原密码不正确", null);
        }
        // 更新数据库
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username", username).set("password", newPassword);
        int result = userMapper.update(null, updateWrapper);

        return result > 0
                ? new LoginResponse(true, "密码修改成功", null)
                : new LoginResponse(false, "修改失败", null);

    }

    //上传头像
    @PostMapping("/uploadAvatar")
    public LoginResponse uploadAvatar(@RequestParam("file") MultipartFile file,
                                      @RequestParam("username") String username,
                                      HttpServletRequest request) {
        if (file.isEmpty()) {
            return new LoginResponse(false, "上传失败，文件为空", null);
        }

        try {
            // 本地路径
            String realPath = request.getServletContext().getRealPath("/uploads/avatar/");
            File dir = new File(realPath);
            if (!dir.exists()) dir.mkdirs();

            // 生成唯一文件名
            String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
            File dest = new File(dir, fileName);
            file.transferTo(dest);

            // 更新数据库
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("username", username).set("image", "/uploads/avatar/" + fileName);
            int result = userMapper.update(null, updateWrapper);

            return result > 0
                    ? new LoginResponse(true, "头像修改成功", null)
                    : new LoginResponse(false, "头像修改失败", null);


        } catch (IOException e) {
            e.printStackTrace();
            return new LoginResponse(false, "服务器错误", null);
        }
    }
}
